package com.keystudio.util;

/**
 * author xing
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.util.Log;

import com.keystudio.model.MessageInfo;

/**
 * 瑙ｆ瀽淇℃伅
 * 
 * @author xing
 * 
 */
public class AnalyticMessageInfo {
	/**
	 * 鏍规嵁message瑙ｆ瀽鐩稿簲鍛戒护
	 * 
	 * @param message
	 * @return
	 */
	public static MessageInfo getMessageInfo(String message) {
		MessageInfo messageInfo = new MessageInfo();
		try {
			String[] str1 = message.split("[{]");
			messageInfo.setCommand(str1[0]);
			str1 = str1[1].split("[}]");
			String[] args = str1[0].split("[,]");
			HashMap<String, String> map = new HashMap<String, String>();
			for (int i = 0; i < args.length; i++) {
				if (!args[i].equals("")) {
					args[i] = args[i].substring(1, args[i].length() - 1);
					String[] c = args[i].split("[:]");
					String key = c[0];
					if (c.length > 1) {
						String value = c[1];
						if (value.contains("|")) {
							String allItems[] = value.split("[|]");
							List items = new ArrayList();
							for (int j = 0; j < allItems.length; j++) {
								for (int k = 0; k < ContantsUtil.nowString.length; k++) {
									allItems[j].replaceAll("["
											+ ContantsUtil.nowString[k] + "]",
											"[" + ContantsUtil.primaryString[k]
													+ "]");
								}
								if (messageInfo.getMoreItems().size() > j&&messageInfo.getMoreItems().get(c[0])!=null) {
									(messageInfo.getMoreItems().get(c[0]))
											.add(allItems[j]);
								} else {
									items.add(allItems[j]);
								}
							}
							messageInfo.getMoreItems().put(c[0], items);
						} else {
							for (int k = 0; k < ContantsUtil.nowString.length; k++) {
								value.replaceAll("["
										+ ContantsUtil.nowString[k] + "]", "["
										+ ContantsUtil.primaryString[k] + "]");
							}
							map.put(key, value);
						}
					}
				}
			}
			messageInfo.setArgs(map);
		} catch (Exception e) {
			Log.e("error", e.getMessage());
			return null;
		}

		return messageInfo;
	}

	/**
	 * 鏍规嵁瑕佷紶鐨勪俊鎭被寰楀埌瑕佷紶鐨勪俊鎭�
	 * 
	 * @param messageInfo
	 * @return
	 */
	public static String getMessage(MessageInfo messageInfo) {
		StringBuffer returnValue = new StringBuffer();
		try {
			if (messageInfo != null) {
				returnValue.append(messageInfo.getCommand());
				returnValue.append("{");
				Iterator iter = messageInfo.getArgs().entrySet().iterator();
				while (iter.hasNext()) {
					Map.Entry entry = (Map.Entry) iter.next();
					Object key = entry.getKey();
					Object val = entry.getValue();
					returnValue.append("\"");
					returnValue.append((String) key);
					returnValue.append(":");
					String value = (String) val;
					for (int k = 0; k < ContantsUtil.nowString.length; k++) {
						value.replaceAll("[" + ContantsUtil.primaryString[k]
								+ "]", "[" + ContantsUtil.nowString[k] + "]");
					}
					returnValue.append(value);
					returnValue.append("\"");
					if (iter.hasNext()) {
						returnValue.append(",");
					}
				}
				if (messageInfo.getMoreItems().size() > 0) {
					if (messageInfo.getArgs().size() > 0) {
						returnValue.append(",");
						Iterator iterItems = messageInfo.getMoreItems()
								.entrySet().iterator();
						while (iterItems.hasNext()) {
							Map.Entry entry = (Map.Entry) iterItems.next();
							Object key = entry.getKey();
							Object val = entry.getValue();
							returnValue.append("\"");
							returnValue.append((String) key);
							returnValue.append(":");
							List valList = (List) val;
							for (int j = 0; j < valList.size(); j++) {
								String value = (String) valList.get(j);
								for (int k = 0; k < ContantsUtil.nowString.length; k++) {
									value.replaceAll("["
											+ ContantsUtil.primaryString[k]
											+ "]", "["
											+ ContantsUtil.nowString[k] + "]");
								}
								returnValue.append(value);
								if (j < valList.size() - 1) {
									returnValue.append("|");
								}
							}
							returnValue.append("\"");
							if (iterItems.hasNext()) {
								returnValue.append(",");
							}
						}
					}
				}
				returnValue.append("}\0");
			}
		} catch (Exception e) {
			Log.e("error", e.getMessage());
		}
		return returnValue.toString();
	}
}
